var express = require('express');
var router = express.Router();
const uuid = require("node-uuid");
const goods = require("../sql/goods");
const sql = require("../sql");
const xlsx = require("node-xlsx");
const path = require('path');

let kw;
let s;
let num = 10;
let index = 0;

// 页面路由
router.get('/', function(req, res, next) {
  // 每次打开初始以及路由，将所有全局数据清空
  kw=undefined;
  s=undefined;
  num=10;
  index=0;

  // const p = req.cookies.power
  const p = req.session.power

  // goods的一级路由，初始需要获取数据库中的数据，并传给页面模板，由页面模板进行渲染
  sql.get({
    colName:goods
  }).then(data=>{
    res.render('goods', {
      index: 0,
      data,
      power: p
    });
  })
});

// 页面路由
router.get('/add', function(req, res, next) {
  // const p = req.cookies.power
  const p = req.session.power
  res.render('goodsAdd', {
    index: 0,
    power: p
  });
});

// 功能路由
router.post('/addAction', function(req, res, next) {
  // 已经接收到表单数据
  const data = req.body;
  // 处理数据
  data.price = data.price-0;
  data.stock = data.stock-0;
  data.sales = data.sales-0;
  data.discount = data.discount-0;
  data.tip = data.tip-0;
  data.time = Date.now();
  data.goodsId = "goods-" + uuid.v1();
  // console.log(data);
  // 将数据存储到数据库中
  sql.add(goods, data).then(()=>{
    // 路由重定向：到/goods
    res.redirect("/goods")
  })
});

// 页面路由
router.get('/set', function(req, res, next) {
  // const p = req.cookies.power
  const p = req.session.power
  const id = req.query.id
  sql.get({
    colName:goods,
    where:{
      goodsId: id
    }
  }).then(data=>{
    res.render('goodsSet', {
      index: 0,
      data: data[0],
      power: p
    });
  })
});

// 功能路由
router.post('/setAction', function(req, res, next) {
  // 已经接收到表单数据
  const data = req.body;
  // 处理数据
  data.price = data.price-0;
  data.stock = data.stock-0;
  data.sales = data.sales-0;
  data.discount = data.discount-0;
  data.tip = data.tip-0;
  // 根据id修改数据库中指定的数据
  sql.set(goods, {goodsId: data.goodsId}, data).then(()=>{
    // 路由重定向：到/goods
    res.redirect("/goods");
  })
});

// 功能路由
router.get('/del', function(req, res, next) {
  const id = req.query.id;
  sql.remove(goods, {goodsId: id}).then(data=>{
    res.redirect("/goods");
  })
});

// 功能路由
router.get('/upload', function(req, res, next) {
  // 拼接要导入的文件的路径
  const p = path.join(__dirname, "..", "public", "xlsx", "pro.xlsx");
  // 将路径传给第三方模块，读取文件并获取数据
  const data = xlsx.parse(p)[0].data;
  // 解析成当前数据库所需的数据格式（一定要观察数据的实际格式，做出自定义的解析方式）
  const arr = [];
  for(let i=1;i<data.length;i++){
    arr.push({
      goodsId: "goods-" + uuid.v1(),
      goodsName: data[i][1],
      goodsImg: data[i][5],
      kind: data[i][2],
      brand: data[i][3],
      logo: data[i][4],
      price: data[i][6],
      stock: data[i][8],
      describe: data[i][7],
      discount: data[i][10],
      tip: data[i][11],
      sales: data[i][9],
      time: Date.now()
    })
  }
  // 将数据添加到数据库
  sql.add(goods, arr).then(()=>{
    res.redirect("/goods");
  })
});


// 功能路由
router.get('/search', function(req, res, next) {
  // const p = req.cookies.power
  const p = req.session.power
  kw = req.query.kw;
  const k = new RegExp(kw);
  sql.get({
    colName: goods,
    where: {
      $or: [{goodsName: k}, {describe: k}, {brand: k}, {kind: k}]
    },
    sort: s
  }).then(data=>{
    res.render("goods",{
      index: 0,
      data,
      power: p
    })
  })
});

// 功能路由
router.get('/sort', function(req, res, next) {
  const s = req.query;
  // const p = req.cookies.power
  const p = req.session.power

  sql.getSort(goods, s).then(data=>{
    res.render("goods",{
      index: 0,
      data,
      power: p
    })
  })
});

// 功能路由
router.get('/sort2', function(req, res, next) {
  s = req.query;

  const k = new RegExp(kw);

  // const p = req.cookies.power
  const p = req.session.power
  sql.get({
    colName: goods,
    where: {
      $or: [{goodsName: k}, {describe: k}, {brand: k}, {kind: k}]
    },
    sort: s
  }).then(data=>{
    res.render("goods",{
      index: 0,
      data,
      power: p
    })
  })
});


module.exports = router;
